﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef CMDCTRL_H
#define CMDCTRL_H

#include <QObject>
#include "BurnToolCommon/Global.h"
#include "BurnCtrlFaction.h"
#include "BurnCtrlSle.h"

class CmdCtrl : public QObject {
    Q_OBJECT
public:
    explicit CmdCtrl(QObject *parent = nullptr);
    ~CmdCtrl() override;
    static CmdCtrl* GetInstence();
    UINT InitConfig();
    UINT SleInitConfig();
    void InitConfig3X(BIN_INFO_ARRAY &infoArray, QString &version, QMap<int, BIN_INFO> &mapBinInfo);
    bool YModemType() const;
    bool JlinkType() const;
    bool Type3X() const;
    void SetWifiBurnCtrlPram();
    void SetUsbBurnCtrlPram();
    void InitBurnCtrl();
    void InitConfigSub();
    UINT StartProc();
    void SetBaud(UINT baud);
    void SetBootType(BootType type);
    void SetChipType(ChipType type);
    void SetIsChipType(bool chipType);
    void SetForceReadTime(UINT time);
    void SetConnectTimeout(UINT time);
    void SetEraseMode(int isEraseMode);
    void SetOnlyEraseAll(bool isEraseAll);
    void SetForceRead(bool isForceRead);
    void SetFormal(bool isFormal);
    void SetShow(BOOL isShow);
    void SetOnlyBurnToolLog(bool onlyBurnToolLog);
    void SetPeriod(UINT period);
    void SetBinPath(QString path);
    void SetComNum(UINT comNum);
    void SetAllowBurnFile(QString name);
    void SetResetAfterBurn(bool reset);
    void SetIpAddr(QString ip);
    void SetIpPort(UINT port);
    void SetExportTarget(QString target);
    void SetExportAddr(QString addr);
    void SetExportSize(QString size);
    void SetDfuVid(uint32_t vid);
    void SetDfuPid(uint32_t pid);
    void SetDfuPathID(QString devicePathID);
    void SetDfuLocation(QString deviceLocation);
    void SetDfuUsage(uint32_t usage);
    void SetDfuUsagePage(uint32_t usagePage);
    void SetIsReadEfuse(bool readEfuse);
    void SetStartBit(QString startbit);
    void SetBitWidth(QString bitWidth);
    void SetPackageSize(UINT packageSize);
    void SetSwitchAfterLoader();
    void SetSleAddr(QString str);
    void SetJLinkPath(QString path);
    void SetIsAutoDfu(bool isAutoDfu);
    void SetIsHidDfu(bool isHidDfu);
    void SetIsGetHidDevice(bool isGetHidDevice);
    void SetSleAddrType(QString str);
    int GetResult() const;
    QMap<int, BIN_INFO> SaveTempFile(const QMap<int, BIN_INFO> &binInfo);
    void SetExportAfterBurn(bool isExport);
    void PassParamErrorHint();
    void SlotSetProgress(double d) const;
    void GetHidDevice();

signals:
    void SignalSendMsg(long int msgId, WPARAM wParam, LPARAM lParam);
    void SignalSendMsgSle(long int msgId, WPARAM wParam, LPARAM lParam);
protected slots:
    void SlotOpenComFail();
    void SlotBurnFileFinish();
    void SlotBurnFileFailed();
    void SlotInsertPlainText(const QString &editStr) const;
    void SlotPassParamError(int mode);

protected:
    BurnCtrl *m_burnCtrl = nullptr;
    BurnCtrlSleClass::BurnCtrlSle *m_burnCtrlSle = nullptr;
    ChipType m_chipType = ChipType::WIFI5GNB;
    QSerialPort::Parity m_parity = QSerialPort::NoParity;
    BootType m_bootType = BOOT_ROM;
    BOOL m_isSecConn = false;
    QString m_strPath = "";
    UINT m_comNum = 0;
    UINT m_baud = 115200; // 波特率115200
    UINT m_databits = 8;  // 数据位8
    UINT m_stopbits = 0;
    UINT m_flowCtrl = 0;
    bool m_isFormal = false;
    int m_eraseMode = 0;
    bool m_isEraseAll = false;
    UINT m_period = 10; // period 10 ms
    BOOL m_isForceRead = FALSE;
    BOOL m_onlyBurnToolLog = FALSE;
    UINT m_forceReadTime = 0;
    UINT m_connectTime = 60000;
    BOOL m_isShow = FALSE;
    QStringList m_burnFileName;
    bool m_isReset = false;
    QString m_ipAddr;
    UINT m_ipPort = 0;
    bool m_isExport = false;
    QString m_exportTarget = "";
    QString m_exportAddr = "";
    QString m_exportSize = "";
    QString m_startBit = "";
    QString m_bitWidth = "";
    bool isEncrypt = false;
    ENCRYPT_BUFFER encryptBuffer;
    uint32_t m_vid = 0;
    uint32_t m_pid = 0;
    QString m_usbDevicePathID = "";
    QString m_usbLocation = "";
    uint32_t m_usage = 0;
    uint32_t m_usagePage = 0;
    QString m_jLinkPath = "";
    bool m_readEfuse = false;
    bool m_isAutoDfu = false;
    bool m_isHidDfu = false;
    UINT m_packageSize = 1024;
    bool m_switchAfterLoader = false;
    SleDeviceInfo m_sleDeviceInfo;
    QString m_strSleAddr = "";
    QString m_strSleAddrType = "";
    bool m_isGetHidDevice = false;
    MyStringClass::StringClass stringClass;
    bool m_isChipType = false;

private:
    void RemoveTempDir() const;
    void InitConfigBinInfo(const BIN_INFO_ARRAY &infoArray, QMap<int, BIN_INFO> &mapBinInfo) const;
    CHAR *HidEnumerateInterface(HidDeviceInfo deviceInfo) const;
};

#endif // CMDCTRL_H
